using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text;

namespace Kenly.DBFramework.ORMCodeHelper
{
    [Serializable]
    public class UserSettings
    {
        private string _selectedDataSource;
        private string _defaultNameSpace;
        private string _outputDir;
        private string _selectedTemplate;
        private bool _addAttribute;
        private bool _addProjectFiles;

        public string SelectedDataSource
        {
            get
            {
                return _selectedDataSource;
            }
            set
            {
                _selectedDataSource = value;
            }
        }

        public string DefaultNameSpace
        {
            get
            {
                return _defaultNameSpace;
            }
            set
            {
                _defaultNameSpace = value;
            }
        }

        public string OutputDir
        {
            get
            {
                return _outputDir;
            }
            set
            {
                _outputDir = value;
            }
        }

        public string SelectedTemplate
        {
            get
            {
                return _selectedTemplate;
            }
            set
            {
                _selectedTemplate = value;
            }
        }

        public bool AddAttribute
        {
            get
            {
                return _addAttribute;
            }
            set
            {
                _addAttribute = value;
            }
        }

        public bool AddProjectFiles
        {
            get
            {
                return _addProjectFiles;
            }
            set
            {
                _addProjectFiles = value;
            }
        }

        public string ConnectionString
        {
            get
            {
                List<DataSource> dataSources = DataSource.Deserialize(PathConst.DataSourceFilePath);
                if (dataSources == null || dataSources.Count <= 0)
                {
                    return "";
                }

                foreach (DataSource item in dataSources)
                {
                    if (item.Key == this.SelectedDataSource)
                    {
                        return item.ConnectionString;
                    }
                }

                return "";
            }
        }

        public static void Serialize(string filePath,UserSettings userSettings)
        {
            ConfigSerializer.Serialize(filePath, userSettings);
        }

        public static UserSettings Deserialize(string filePath)
        {
            object obj = ConfigSerializer.Deserialize(filePath, typeof(UserSettings));
            UserSettings userSettings=new UserSettings();

            if (obj == null)
            {
                return userSettings;
            }

            userSettings = obj as UserSettings;

            return userSettings;

        }
    }
}
